package com.pig4cloud.pig.module.controller.attendance;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.attendance.AttendanceGroup;
import com.pig4cloud.pig.module.api.entity.attendance.AttendanceOffWork;
import com.pig4cloud.pig.module.service.attendance.IAttendanceGroupService;
import com.pig4cloud.pig.module.service.attendance.IAttendanceOffWorkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 考勤的 调休假期Controller
 *
 * @author ruoyi
 * @date 2022-10-08
 */
@Validated
@Api(value = "考勤的 调休假期控制器", tags = {"考勤的 调休假期管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/attendance/work")
public class AttendanceOffWorkController {

    private final IAttendanceOffWorkService attendanceOffWorkService;
    private final IAttendanceGroupService attendanceGroupService;

    /**
     * 查询考勤的 调休假期列表
     */
    @ApiOperation("查询考勤的 调休假期列表")
    @PreAuthorize("@pms.hasPermission('module:attendanceWork:list')")
    @GetMapping("/list")
    public TableDataInfo<AttendanceOffWork> list(@Validated AttendanceOffWork attendanceOffWork) {
        QueryWrapper<AttendanceOffWork> qw = new QueryWrapper<>();
        if(attendanceOffWork.getGroupId()!=null){
            qw.eq("group_id",attendanceOffWork.getGroupId());
        }
        TableDataInfo<AttendanceOffWork> td = PageUtils.buildDataInfo(attendanceOffWorkService.page(PageUtils.buildPage(),qw));
        List<AttendanceOffWork> list = td.getData();
        for (AttendanceOffWork list1:list){
            if(list1.getGroupId()!=null&&list1.getGroupId()!=0){
                AttendanceGroup ag=attendanceGroupService.findById(list1.getGroupId());
                if (ag!=null){
                    list1.setGroupName(ag.getGroupName());
                }
            }
        }
        return td;
    }

    /**
     * 导出考勤的 调休假期列表
     */
    @ApiOperation("导出考勤的 调休假期列表")
    @PreAuthorize("@pms.hasPermission('module:attendanceWork:export')")
    @SysLog("考勤的 调休假期")
    @GetMapping("/export")
    public List export(@Validated AttendanceOffWork attendanceOffWork) {
        List<AttendanceOffWork> list = attendanceOffWorkService.list();
        // ExcelUtil<AttendanceOffWork> util = new // ExcelUtil<AttendanceOffWork>(AttendanceOffWork.class);
        return  list;// util.exportExcel(list, "考勤的 调休假期");
    }

    /**
     * 获取考勤的 调休假期详细信息
     */
    @ApiOperation("获取考勤的 调休假期详细信息")
    @PreAuthorize("@pms.hasPermission('module:attendanceWork:query')")
    @GetMapping("/{id}")
    public R<AttendanceOffWork> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        AttendanceOffWork attendanceOffWork = attendanceOffWorkService.getById(id);
        return R.ok(attendanceOffWork);
    }

    /**
     * 新增考勤的 调休假期
     */
    @ApiOperation("新增考勤的 调休假期")
    @PreAuthorize("@pms.hasPermission('module:attendanceWork:add')")
    @SysLog("考勤的 调休假期")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody AttendanceOffWork attendanceOffWork) {
        attendanceOffWork.setIsDefault(0);
        return R.okByRow(attendanceOffWorkService.save(attendanceOffWork) ? 1 : 0);
    }

    /**
     * 修改考勤的 调休假期
     */
    @ApiOperation("修改考勤的 调休假期")
    @PreAuthorize("@pms.hasPermission('module:attendanceWork:edit')")
    @SysLog("考勤的 调休假期")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody AttendanceOffWork attendanceOffWork) {
        return R.okByRow(attendanceOffWorkService.updateById(attendanceOffWork) ? 1 : 0);
    }

    /**
     * 删除考勤的 调休假期
     */
    @ApiOperation("删除考勤的 调休假期")
    @PreAuthorize("@pms.hasPermission('module:attendanceWork:remove')")
    @SysLog("考勤的 调休假期")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(attendanceOffWorkService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
